import router, { defaultRoutes } from '@/router/index'

import Main from '@/layout'

export const getRoutes = children => {
  const routes = []
  children.forEach((sub) => {
    routes.push({
      path: sub.path,
      name: sub.name,
      meta: sub.meta,
      //component: () => import(`@/views${sub.component}.vue`)
      component: resolve => require([`@/views${sub.component}.vue`], resolve)
    })
  })
  return routes
  // return arr.map(({path, name, component, meta, children}) => {
  //   const route = {
  //     path,
  //     name,
  //     meta: {
  //       ...meta,
  //       require: true
  //     },
  //     component: () => import(`${component}.vue`)
  //   };
  //   return route;
  // });
}
export const setRoutes = list => {
  const routes = []
  list.forEach((sub) => {
    const rt = {
      path: sub.path,
      name: sub.name,
      meta: sub.meta,
      component: Main,
      children: getRoutes(sub.children)
    }
    routes.push(rt)
  })
  router.addRoutes(routes)
  // 路由 options 并不会随着 addRoutes 动态响应，所以要在这里进行设置
  router.options.routes = defaultRoutes.concat(routes)
  routes.forEach(element => {
    defaultRoutes.push(element)
  })
  routes.push({ path: '/home' })
}

export const resetRoutes = () => {
  // 路由 options 并不会随着 addRoutes 动态响应，所以要在这里进行设置
  router.options.routes = defaultRoutes
}
